# Copyright 2011 Google Inc. All Rights Reserved.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from lib.py.belay import *


def keyName(key):
  if isinstance(key, str):
    return key
  return key.name()
  
def launch_url(stationKey):
  return server_url('/belay/launch?s=' + keyName(stationKey))
  
def instances_url(stationKey):
  return server_url('/instances?s=' + keyName(stationKey))

def instance_url(stationKey, instanceKey):
  return server_url('/instance?s=' + keyName(stationKey)
    + '&i=' + keyName(instanceKey))

def tool_url(port, path, https=True):
  # as this utility generates urls to other applications, it is
  # safer to base the http/https decision on whether we are running
  # on the dev appengine runtime. We should be using https all the
  # time in the real environment.
  if os.environ['SERVER_SOFTWARE'].startswith('Development'):
    proto = 'http'
  else:
    proto = 'https'

  return "%s://%s:%d%s" % (proto, os.environ['SERVER_NAME'], port, path)

def cap(url):
  return { '@': url }
